<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


// ------------------------------------------------------------------------


/**
 * escrow trasaction
 *
 * Create a admin URL based on the admin folder path mentioned in config file. Segments can be passed via the
 * first parameter either as a string or an array.
 *
 * @access	public
 * @param	string
 * @return	string
 */
	//Some common fucntion to load escrow only 		
	function loadTransaction($creator_condition,$transaction_condition,$url,$page)
	{		   
			//Get Transaction Information
			$CI 	=& get_instance();
			$CI->load->model('transaction_model');			
			$CI->load->model('user_model');			
			$transactions 	 = $CI->transaction_model->getTransactions($creator_condition);
			$CI->outputData['transactions'] = $transactions;
			//echo $transactions->num_rows();
			//Get Transaction Information
			 $start = $page;
			 
			 //Get all the site settings
			 
			 //$CI->outputData['escrow_limit']       =   $CI->config->item('escrow_page_limit');
			 //$CI->outputData['transaction_limit']  =   $CI->config->item('transaction_page_limit'); 
			 
			
			 $page_rows         =  $CI->config->item('transaction_page_limit'); 
			 
			 if($start > 0)
				$start = ($start-1) * $page_rows;
				
			 //escrow without limit
			 
			 $escrow_transactions 	 = $CI->transaction_model->getTransactions($transaction_condition);
			 $CI->outputData['transactions1'] = $escrow_transactions;
			 
			 //escrow trasaction with some limit	 		 
			 
			 $limit[0]			 = $page_rows;
			 $limit[1]			 = $start;
			 
			 $transactions1 	 = $CI->transaction_model->getTransactions($transaction_condition,NULL,NULL,$limit);
			 $CI->outputData['transactions1'] = $transactions1;
			 $transactions1->num_rows();
		
			  //Pagination
			 $CI->load->library('pagination');
			 $config['base_url'] 	 = site_url($url);
			 $config['total_rows'] 	 = $escrow_transactions->num_rows();		
			 $config['per_page']     = $page_rows; 
			 $config['cur_page']     = $start;
			 $CI->pagination->initialize($config);		
			 $CI->outputData['pagination']   = $CI->pagination->create_links2(false,'accounts');
			
			 //Get all users info
			 $usersList   =   $CI->user_model->getUserslist();
			 $CI->outputData['usersList']    =   $usersList;
			
			 //Get all the projects details
			 $projectList   =   $CI->skills_model->getUsersproject();
			 $CI->outputData['projectList']    =   $projectList;
			return $CI->outputData;
	}
	
	function getSuspendStatus($userid)  
	{
		$CI 	=& get_instance();
		$CI->load->model('common_model');
		$condition =array('users.id'=>$userid);
		$sus_status= $CI->common_model->getTableData('users',$condition,'users.suspend_status');
		$sus_status = $sus_status->row();
		return $sus_status->suspend_status;
	}
	
	/**
	 * insert if user_id not exists
	 */
	if ( ! function_exists('insertNotExists')){
		function insertNotExists($userId){
			$CI 	=& get_instance();
			$CI->load->model('transaction_model');
			$condition =array('user_balance.user_id'=>$userId);
			$result = $CI->transaction_model->getBalance($condition, "user_balance.user_id");
			
			if($result->num_rows() <= 0){
				$CI->transaction_model->addBalance(array('user_id' => $userId ));
			}
		}
	}
	
	if ( ! function_exists('getAmountBalance')){
		function getAmountBalance($userId){
			$CI 	=& get_instance();
			$CI->load->model('transaction_model');
			$condition =array('user_balance.user_id'=>$userId);
			$balances = $CI->transaction_model->getBalance($condition, "user_balance.amount");
			if($balances != null){
				$balance = $balances->row();
				$result = $balance->amount;
			}else{
				$result = 0;
			}

			return $result;
		}
	}

    if( ! function_exists('getExperienceBalance')){
        function getExperienceBalance($userId){
            $CI = &get_instance();
            $CI->load->model('transaction_model');
            $conditions = array('user_id' => $userId);
            $fields = array('experience');

            $result = $CI->transaction_model->getBalance($conditions,$fields);
            return $result->row()->experience;
        }
    }
	
	if ( ! function_exists('getFrozenBalance')){
		function getFrozenBalance($userId){
			$CI 	=& get_instance();
			$CI->load->model('transaction_model');
			$condition =array('user_balance.user_id'=>$userId);
			$balances = $CI->transaction_model->getBalance($condition, "user_balance.frozen");
			if($balances != null){
				if($balances->num_rows() > 0){
					$balance = $balances->row();
					$result = $balance->frozen;
				}else{
					$result = 0;
				}				
			}else{
				$result = 0;
			}
			
			return $result;
		}
	}
	
	if ( ! function_exists('getBonusBalance')){
		function getBonusBalance($userId){
			$CI 	=& get_instance();
			$CI->load->model('transaction_model');
			$condition =array('user_balance.user_id'=>$userId);
			$balances = $CI->transaction_model->getBalance($condition, "user_balance.bonus");
			if($balances != null){
				$balance = $balances->row();
				$result = $balance->bonus;
			}else{
				$result = 0;
			}
				
			return $result;
		}
	}
	
	/** 
	 * transfer amount to frozen
	 */
	if ( ! function_exists('debitAmount')){
		function debitAmount($userId, $amount){
			insertNotExists($userId);

            $CI 	=& get_instance();
			$CI->load->model('transaction_model');
			$CI->load->library('log_balance');
			
			$condition =array('user_balance.user_id'=>$userId);
			$result = $CI->transaction_model->getBalance($condition, "amount");
			$balance = $result->row();
			
			$amountNew = $balance->amount - $amount;
			
			$dataUpdate = array(
				'amount' => $amountNew,
			);
			
			try {
				$resultUpdate = $CI->transaction_model->updateBalance($condition, $dataUpdate);
				$CI->log_balance->addLogAmount($userId, $amount,$balance->amount,2);
			
				return $resultUpdate;
			
			}catch (Exception $e){
				throw $e;
				return FALSE;
			}
		}
	}

	/**
	 * update amount
	 */
	if(! function_exists('updateAmount')){
		function updateAmount($userId,$amount){
			insertNotExists($userId);
			$CI = & get_instance();
			$CI->load->model('transaction_model');
			$CI->load->library('log_balance');
			
			$condition =array('user_balance.user_id'=>$userId);
			$old_score = $CI->transaction_model->getBalance($condition);
			$balance = $old_score->row();
			
			$old_score = $old_score->row()->amount;
			$new_score = $old_score + $amount;

			$dataUpdate = array(
				'amount' => $new_score,
				//"created" => $created
			);
			
			try {
				$resultUpdate = $CI->transaction_model->updateBalance($condition, $dataUpdate);
				$CI->log_balance->addLogAmount($userId, $amount,$balance->amount,1);
					
				return $resultUpdate;
					
			}catch (Exception $e){
				throw $e;
				return FALSE;
			}
		}
	}

	/**
	 * insert amount history
	 */
	if(! function_exists('insertAmountHistory')){
		function insertAmountHistory($userId,$new_amount,$created){
			$CI = & get_instance();
			$CI->load->model('transaction_model');
			$condition =array('user_balance.user_id'=>$userId);			
			$old_score = $CI->transaction_model->getBalance($condition);
            if($old_score->num_rows() > 0)
			$old_score = $old_score->row()->amount;
            else
                $old_score = 0;

			$new_score = $old_score + $new_amount;
			$dataUpdate = array(
				'amount_new' => $new_score,
				'amount_old' => $old_score,
				"created" => $created,
				'user_id' => $userId
			);

			return $CI->transaction_model->insertHistoryBalance($dataUpdate);

		}
	}

	/**
	 * get amount history
	 */
	if(! function_exists('insertAmountHistory')){
		function getAmountHistory($userId){
			$CI = & get_instance();
			$CI->load->model('transaction_model');
			$condition =array('history_balance.user_id'=>$userId);			
			
			return $CI->transaction_model->getHistoryBalance($condition);
		}
	}
	
	/**
	 * credit amount
	 */
	if ( ! function_exists('creditAmount')){
		function creditAmount($userId, $amount){
			insertNotExists($userId);
				
			$CI = & get_instance();
			$CI->load->model('transaction_model');
			$CI->load->library('log_balance');
			
			$condition =array('user_balance.user_id'=>$userId);
			$result = $CI->transaction_model->getBalance($condition, "amount");
			$balance = $result->row();
			$amountNew = $balance->amount + $amount;
	
			$dataUpdate = array(
					'amount' => $amountNew
			);
			
			try {
				$resultUpdate = $CI->transaction_model->updateBalance($condition, $dataUpdate);
				$CI->log_balance->addLogAmount($userId, $amount,$balance->amount,2);
				
				return $resultUpdate;
				
			}catch (Exception $e){
				throw $e;
				return FALSE;
			}
		}
	}
	
	/**
	 * transfer amount to frozen
	 */
	if ( ! function_exists('debitAmountToFrozen')){
		function debitAmountToFrozen($userId, $amount){
			insertNotExists($userId);
			
			$CI 	=& get_instance();
			$CI->load->model('transaction_model');
			$CI->load->library('log_balance');
			
			$condition =array('user_balance.user_id'=>$userId);
			$result = $CI->transaction_model->getBalance($condition, "amount, frozen");
			$balance = $result->row();
			$amountNew = $balance->amount - $amount;
			$frozenNew = $balance->frozen + $amount;
				
			$dataUpdate = array(
					'amount' => $amountNew,
					'frozen' => $frozenNew
			);
			try {
				$resultUpdate = $CI->transaction_model->updateBalance($condition, $dataUpdate);
				$CI->log_balance->addLogAmount($userId, $amount,$balance->amount,2);
				$CI->log_balance->addLogFrozen($userId, $amount,$balance->frozen,1);
					
				return $resultUpdate;
					
			}catch (Exception $e){
				throw $e;
				return FALSE;
			}
		}
	}
	
	/**
	 * debit frozen
	 */
	if ( ! function_exists('debitFrozen')){
		function debitFrozen($userId, $frozen){
			insertNotExists($userId);
			
			$CI 	=& get_instance();
			$CI->load->model('transaction_model');
			$CI->load->library('log_balance');
			
			$condition =array('user_balance.user_id'=>$userId);
			$result = $CI->transaction_model->getBalance($condition, "frozen");
			$balance = $result->row();
			
			$frozenNew = $balance->frozen - $frozen;
	
			$dataUpdate = array(
					'frozen' => $frozenNew
			);
	
			try {
				$resultUpdate = $CI->transaction_model->updateBalance($condition, $dataUpdate);
				$CI->log_balance->addLogFrozen($userId, $frozen,$balance->frozen,2);
					
				return $resultUpdate;
					
			}catch (Exception $e){
				throw $e;
				return FALSE;
			}
		}
	}
	
	/**
	 * debit bonus
	 */
	if ( ! function_exists('debitBonus')){
		function debitBonus($userId, $bonus){
			insertNotExists($userId);
			
			$CI = & get_instance();
			$CI->load->model('transaction_model');
			$condition =array('user_balance.user_id'=>$userId);
			$result = $CI->transaction_model->getBalance($condition, "bonus");
			$balance = $result->row();
			
			$bonusNew = $balance->bonus - $bonus;
	
			$dataUpdate = array(
					'bonus' => $bonusNew
			);

            if($CI->transaction_model->updateBalance($condition, $dataUpdate)){
                $CI->load->library('log_balance');
                $CI->log_balance->addLogBonus($userId,$balance,$result->bonus,2);
                return TRUE;
            }else{
                return FALSE;
            }
		}
	}
	
	/**
	 * credit bonus
	 */
	if ( ! function_exists('creditBonus')){
		function creditBonus($userId, $bonus){
			insertNotExists($userId);
			
			$CI = & get_instance();
			$CI->load->model('transaction_model');
			$condition =array('user_balance.user_id'=>$userId);
			$result = $CI->transaction_model->getBalance($condition, "bonus");
			$balance = $result->row();
			$bonusNew = $balance->bonus + $bonus;
	
			$dataUpdate = array(
					'bonus' => $bonusNew
			);

            if($CI->transaction_model->updateBalance($condition, $dataUpdate)){
                $CI->load->library('log_balance');
                $CI->log_balance->addLogBonus($userId,$bonusNew,$balance->bonus,1);
                return TRUE;
            }else{
                return FALSE;
            }

		}
	}

/**
 * update amount
 */
if(! function_exists('updateFrozen')){
    function updateFrozen($userId,$frozen){
        insertNotExists($userId);
        $CI = & get_instance();
        $CI->load->model('transaction_model');
        $CI->load->library('log_balance');
        
        $condition =array('user_balance.user_id'=>$userId);
        $fields = array('frozen');


        $old_score = $CI->transaction_model->getBalance($condition,$fields);
        $balance = $old_score->row();
        
        $old_score = $old_score->row()->frozen;
        $new_score = $old_score + $frozen;

        $dataUpdate = array(
            'frozen' => $new_score,
            //"created" => $created
        );

        try {
        	$resultUpdate = $CI->transaction_model->updateBalance($condition, $dataUpdate);
        	$CI->log_balance->addLogFrozen($userId, $frozen,$balance->frozen,1);
        		
        	return $resultUpdate;
        		
        }catch (Exception $e){
        	throw $e;
        	return FALSE;
        }
    }
}

if ( ! function_exists('creditExperience')){
    function creditExperience($userId, $score){
        insertNotExists($userId);

        $CI = & get_instance();
        $CI->load->model('transaction_model');
        $condition =array('user_balance.user_id'=>$userId);
        $result = $CI->transaction_model->getBalance($condition, "experience");
        $experience = $result->row();
        $experienceNew = $experience->experience + $score;

        $dataUpdate = array(
            'experience' => $experienceNew
        );

        if($CI->transaction_model->updateBalance($condition, $dataUpdate)){
            $CI->load->library('log_balance');
            $CI->log_balance->addLogExperience($userId,$experienceNew,$experience->experience,1);
            return TRUE;
        }else{
            return FALSE;
        }
    }
}

if ( ! function_exists('debitExperience')){
    function debitExperience($userId, $score){
        insertNotExists($userId);

        $CI = & get_instance();
        $CI->load->model('transaction_model');
        $condition =array('user_balance.user_id'=>$userId);
        $result = $CI->transaction_model->getBalance($condition, "experience");
        $experience = $result->row();
        $experienceNew = $experience->experience - $score;

        $dataUpdate = array(
            'experience' => $experienceNew
        );

        if($CI->transaction_model->updateBalance($condition, $dataUpdate)){
            $CI->load->library('log_balance');
            $CI->log_balance->addLogExperience($userId,$score,$experience->experience,2);
            return TRUE;
        }else{
            return FALSE;
        }

    }
}







/* End of file users_helper.php */
/* Location: ./app/helpers/users_helper.php */
?>